package org.weed.gadgets;

import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.Panel;
import java.awt.PopupMenu;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.JOptionPane;
import javax.swing.plaf.metal.MetalIconFactory;

public class AppStart {
	private static Image getTrayIcon() throws HeadlessException {
        File imageFile = new File("icon.png");
        Image image = null;
		try {
			image = ImageIO.read(imageFile);
		} catch (IOException e) {
	        Icon defaultIcon = MetalIconFactory.getTreeComputerIcon();
	        image = new BufferedImage(defaultIcon.getIconWidth(), defaultIcon.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR); 
	        defaultIcon.paintIcon(new Panel(), image.getGraphics(), 0, 0);
		}
        

        return image;
    }
 
	private static PopupMenu createPopupMenu() throws HeadlessException {
        PopupMenu menu = new PopupMenu();
        File dir = new File(".");
        for (File child : dir.listFiles()) {
        	if (child.isHidden()) continue;
        	int extensionIndex = child.getName().lastIndexOf(".");
        	if (extensionIndex < 0) {
        		menu.add(getAppLink(child.getName(), child.getAbsolutePath()));
        	} else {
        		menu.add(getAppLink(child.getName().substring(0, extensionIndex), child.getAbsolutePath()));
        	}
        }
        
        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(new ActionListener() {        	
	        public void actionPerformed(ActionEvent e) {
	        	System.exit(0);
	        }
        });
        menu.add(exit);
        
        PopupMenu subMenu = new PopupMenu("test.....");
        subMenu.add(getAppLink("1","a"));
        menu.add(subMenu);
        return menu;
    }
	
	
	
	private static PopupMenu getPopupMenu(String name, File folder) {
		PopupMenu menu = new PopupMenu(name);
		List<File> files = Arrays.asList(folder.listFiles());
		Collections.sort(files, new Comparator<File>() {
			public int compare(File file1, File file2) {
				if ((file1.isDirectory() && file2.isDirectory()) || (!file1.isDirectory() && !file2.isDirectory())) {
					return file1.getName().compareTo(file2.getName());
				} else {
					return file1.isDirectory()? -1 : 1;
				}
			}
		});
		for (File subFile : files) {
			if (subFile.isHidden()) continue;
			if (subFile.isDirectory()) {
				menu.add(getPopupMenu(subFile.getName(), subFile));
			} else {
	        	int extensionIndex = subFile.getName().lastIndexOf(".");
	        	if (extensionIndex < 0) {
	        		menu.add(getAppLink(subFile.getName(), subFile.getAbsolutePath()));
	        	} else {
	        		menu.add(getAppLink(subFile.getName().substring(0, extensionIndex), subFile.getAbsolutePath()));
	        	}
			}
		}
		return menu;
	}
	
	private static MenuItem getAppLink(String name, final String path) {
		MenuItem item = new MenuItem(name);
		item.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				try {
					Process process = Runtime.getRuntime ().exec ("cmd.exe /C " + path);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
        });
		
        return item;
	}
 
	public static void main(String[] args) throws Exception {
		PopupMenu menu = getPopupMenu("top", new File("."));
        MenuItem exit = new MenuItem("Exit");
        exit.addActionListener(new ActionListener() {        	
	        public void actionPerformed(ActionEvent e) {
	        	System.exit(0);
	        }
        });
        menu.add(exit);
        
        TrayIcon icon = new TrayIcon(getTrayIcon(),
                "App Star. Starts your app!", menu);
        icon.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null, "TODO");
           }
        });
        java.awt.SystemTray.getSystemTray().add(icon);
 
    }
}
